

import 'package:flutter/foundation.dart';
import 'package:syria_sports/core/app_export.dart';
import 'package:flutter/material.dart';
import '../bloc/tools_cubit.dart';
import '../../../../data/controllers/gyms_controller.dart';
import '../../../../dialogs/delete_player_confirmation.dart';
import '../models/tool.dart';

class ToolItemView extends StatelessWidget {
  final ToolModel tool;
  final bool isSelected;
  final bool selectOnTap;
  final Function? onTapped;

  const ToolItemView({
    super.key,
    required this.tool,
    this.isSelected = false,
    this.selectOnTap = false,
    this.onTapped,
  });

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: () {
        if (selectOnTap) {
          if (ToolsCubit.get(context).selectMode) {
            ToolsCubit.get(context).add(
              SelectToolEvent(
                tool: tool,
                isSelected: !isSelected,
              ),
            );
          } else {
            ToolsCubit.get(context)
                .add(SelectModeEvent(mode: true, tool: tool));
          }
        } else {
          if (onTapped == null) {
            /*Navigator.push(
                context,
                MaterialPageRoute(
                    builder: (context) => AddSportPageScreen(
                          sport: sport,
                        )));*/
          }else{
            onTapped?.call();
          }
        }
      },
      onLongPress: () {
        if (ToolsCubit.get(context).selectMode) {
          ToolsCubit.get(context).add(
            SelectToolEvent(
              tool: tool,
              isSelected: !isSelected,
            ),
          );
        } else {
          ToolsCubit.get(context)
              .add(SelectModeEvent(mode: true, tool: tool));
        }
      },
      child: Stack(
        children: [
          Card(
            color: Theme.of(context).colorScheme.secondary,
            child: Container(
              padding: const EdgeInsets.all(4),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  if (tool.image != null)
                    Image.network(
                      tool.image!,
                      height: 45,
                    )
                  else
                    Image.asset(
                      ImageConstant.imgLogo,
                      height: 45,
                    ),
                  Expanded(
                    child: Center(
                      child: Text(
                        tool.toolName,
                        maxLines: 2,
                        textAlign: TextAlign.center,
                        style: theme.textTheme.bodyMedium,
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          if (ToolsCubit.get(context).selectMode)
            Row(
              mainAxisAlignment: MainAxisAlignment.start,
              children: [
                Checkbox(
                  value: isSelected,
                  onChanged: (value) {
                    ToolsCubit.get(context).add(
                      SelectToolEvent(
                        tool: tool,
                        isSelected: value ?? false,
                      ),
                    );
                  },
                ),
              ],
            ),
        ],
      ),
    );
  }

  void showDeleteConfirmation(BuildContext context, ToolModel myGym) {
    showDialog(
      context: context,
      builder: (BuildContext context) {
        return DeleteConfirmationDialog(
          playerName: myGym.toolName,
          titleUser: 'الرياضة',
          onConfirm: () {
            GymsController.deleteById(tool.toolId ?? "-1");
            if (kDebugMode) {
              print('تم حذف النادي ${tool.toolName}!');
            }
          },
        );
      },
    );
  }
}
